package com.isunland.managesystem.ui;

import android.os.Bundle;
import com.google.gson.Gson;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.DictionaryTreeContent;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/7/26 17:56
 */
public class BuyPurposeDialogFragment extends BaseNetworkDialogFragment {
  public static final String EXTRA_FROM =
      BuyPurposeDialogFragment.class.getSimpleName() + "EXTRA_FROM";
  public static final String QUERY = "query";
  public static final String DETAIL = "detail";
  private String from;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    from = getArguments().getString(EXTRA_FROM);
  }

  @Override public ArrayList<CustomerDialog> resolveResponse(String arg0) {
    ArrayList<CustomerDialog> list = new ArrayList<>();

    DictionaryTreeContent[] dataList = new Gson().fromJson(arg0, DictionaryTreeContent[].class);

    if (dataList == null || dataList.length == 0) {

      return null;
    }
    for (DictionaryTreeContent aDataList : dataList) {
      list.add(new CustomerDialog(aDataList.getName(), aDataList.getCustomAttrs()));
    }
    if (QUERY.equalsIgnoreCase(from)) {
      list.add(0,new CustomerDialog("全部", ""));
    }
    return list;
  }

  @Override public HashMap<String, String> getparameters() {

    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("dictFlg", "oaBuyPurpose");
    return params.getParams();
  }

  @Override public String getUrl() {
    return ApiConst.URL_DATA_DICTIONARY_LIST;
  }

  public static BuyPurposeDialogFragment newInstance(String from) {
    Bundle args = new Bundle();
    args.putString(EXTRA_FROM, from);
    BuyPurposeDialogFragment fragment = new BuyPurposeDialogFragment();
    fragment.setArguments(args);
    return fragment;
  }
}

